//
//  Hud.m
//  Maze
//
//  Created by Lin Tian on 9/13/11.
//  Copyright 2011 tina. All rights reserved.
//

#import "Hud.h"

@implementation Hud

@synthesize scoreLabel;
@synthesize itemList;
@synthesize player;
@synthesize itemToRemove;

+(CCScene*)scene
{
    CCScene* scene = [CCScene node];
    
    Hud* aHud = [Hud node];
    
    [scene addChild:aHud];
    
    return scene;
}

-(id)initWithPlayer:(LocalPlayer*)aPlayer
{
    if((self = [super init])){
        self.player = aPlayer;
        /*scoreLabel = [CCLabelTTF labelWithString:@"0" 
                                      dimensions:CGSizeMake(50, 20) 
                                       alignment:UITextAlignmentRight 
                                        fontName:@"Verdana-Bold"
                                        fontSize:12];
        CGSize winSize = [[CCDirector sharedDirector] winSize];
        scoreLabel.position = ccp(winSize.width - scoreLabel.contentSize.width, winSize.height - scoreLabel.contentSize.height);
        [self addChild:scoreLabel];*/
        
        CCMenuItemImage* item1 = [CCMenuItemImage itemFromNormalImage:@"iphone_settings.png" 
                                                        selectedImage:@"iphone_settings.png" 
                                                               target:self 
                                                             selector:@selector(transit:)];
        item1.tag = 1;
        
        CCMenu *aMenu= [CCMenu menuWithItems:item1, nil];
        aMenu.position = ccp(1000,740);
        [self addChild:aMenu z:0 tag:SETTINGMENU];
        
        aMenu=[CCMenu menuWithItems:nil];
        aMenu.position = ccp(980,500);
        [aMenu alignItemsVertically];
        [self addChild:aMenu z:0 tag:ITEMLIST];
        
        self.itemList = [NSMutableArray array];
        self.itemToRemove = [NSMutableArray array];
        
        [self schedule:@selector(removeIcon:) interval:4];
        
    }
    return self;
}

-(void)release
{
    [itemList release];
    itemList = nil;
    
    [super release];
}

-(void)transit:(CCMenuItem*)sender
{
    int i = sender.tag;
    switch (i) {
        case 1:
        {
            [self.parent addChild:[[[Menu alloc] init] autorelease] z:4 tag:3];
            break;
        }
        default:
            break;
    }
    
}

-(void)useItem:(CCMenuItem*)sender
{
    int i = sender.tag;
    Item* anItem = [self.itemList objectAtIndex:i];
    if (anItem) {
        [anItem useItemBy:self.player];
    }
    [self.itemList removeObject:anItem];
    CCMenu* aMenu = (CCMenu*)[self getChildByTag:ITEMLIST];
    CCMenuItemImage* icon;
    for (int j = i; j<[self.itemList count]; j++) {
        icon = (CCMenuItemImage*)[aMenu getChildByTag:j+1];
        icon.tag = j;
        icon.position = CGPointMake(0, -icon.tag*icon.contentSize.height);
    }
    //[sender.parent removeChild:sender cleanup:YES];
    
    sender.visible = NO;
    [self.itemToRemove addObject:sender];
}

-(BOOL)addItemIcon:(Item*)anItem
{
    if ([self.itemList count] < 2) {
        [self.itemList addObject:anItem];
        CCMenuItemImage* icon = [CCMenuItemImage itemFromNormalImage:anItem.texture
                                                       selectedImage:anItem.texture
                                                              target:self
                                                            selector:@selector(useItem:)];
        icon.tag = [self.itemList count]-1;
        icon.position = CGPointMake(0, -icon.tag*icon.contentSize.height);
        [[self getChildByTag:ITEMLIST] addChild:icon];
        return YES;
    }
    return NO;
}

-(void)removeIcon:(ccTime)dt
{
    for (int i =0; i<[self.itemToRemove count]; i++) {
        [[self getChildByTag:ITEMLIST] removeChild:[self.itemToRemove objectAtIndex:i] cleanup:YES];
    }
}

@end
